package nape.util;
import DummyNapeMain;
import nape.Config;
import nape.Const;
import nape.dynamics.Arbiter;
import nape.dynamics.Contact;
import nape.geom.AABB;
import nape.geom.Axis;
import nape.geom.Mat23;
import nape.geom.Vec2;
import nape.geom.VecMath;
import nape.phys.Material;
import nape.shape.Circle;
import nape.shape.Polygon;
import nape.shape.Shape;
import nape.util.FastMath;
import nape.util.MixPool;
import nape.util.Names;
import nape.util.NpAlgorithm;
import nape.util.NpAllocList_ZNContact;
import nape.util.NpNode_ZNV2;
import nape.util.NpList_ZNV2;
import nape.util.NpNode_Vec2__Dynamic;
import nape.util.NpList_Vec2__Dynamic;
import nape.util.NpNode_ZNShp;
import nape.util.NpList_ZNShp;

//'newfile' generated imports
import nape.util.NpNode_ZNContact;
import nape.util.NpAllocList_ZNContact;

class NpAllocList_ZNContact {
    public var head:NpNode_ZNContact;
    public function new() {}
    
    public inline function begin() return head
    
    
    
    
    
    
    
    
    
    public inline function add(o:ZNContact) {
        var temp =  {
        var ret =  {
    if(NpNode_ZNContact.pool==null) new NpNode_ZNContact();
    else {
        var ret = NpNode_ZNContact.pool;
        NpNode_ZNContact.pool = ret.pool_next;
        ret;
    }
};
        ret.elt = o;
        ret;
    };
        temp.next = begin();
         head=temp;
    }
    
    
    public inline function addAll(x:NpAllocList_ZNContact) {
           {
    var npiterator = x.begin();
    while(npiterator != null) {
        var i = npiterator.elem();
        {
            
            
            add(i);
        }
        npiterator = npiterator.next;
    }
};
    }
    
    
    public inline function insert(cur:NpNode_ZNContact,o:ZNContact) {
        if(cur==null) { add(o); return begin(); }
        else {
            var temp =  {
        var ret =  {
    if(NpNode_ZNContact.pool==null) new NpNode_ZNContact();
    else {
        var ret = NpNode_ZNContact.pool;
        NpNode_ZNContact.pool = ret.pool_next;
        ret;
    }
};
        ret.elt = o;
        ret;
    };
            temp.next = cur.next;
            cur.next = temp;
            return temp;
        }
    }
    
    
    
    
    public inline function pop():Void {
        var ret = begin();
         head=ret.next;
          {
    ret.elem().free();
    ret.elem().pool_next = ZNContact.pool;
    ZNContact.pool = ret.elem();
};
          {
    ret.free();
    ret.pool_next = NpNode_ZNContact.pool;
    NpNode_ZNContact.pool = ret;
};
    }
    
    
    public inline function pop_unsafe():ZNContact {
        var ret = front();
        pop();
        return ret;
    }
    
    
    
    
    public inline function remove(obj:ZNContact):Bool {
        var pre = null;
        var cur = begin();
        var ret = false;
        while(cur!=null) {
            if(cur.elem()==obj) {
                erase(pre,cur);
                ret = true;
                break;
            }
            pre = cur;
            cur = cur.next;
        }
        return ret;
    }
    
    
    public inline function erase(pre:NpNode_ZNContact,cur:NpNode_ZNContact):NpNode_ZNContact {
        var old = cur; cur = cur.next;
        if(pre==null)  head=cur;
        else pre.next = cur;
          {
    old.elem().free();
    old.elem().pool_next = ZNContact.pool;
    ZNContact.pool = old.elem();
};
          {
    old.free();
    old.pool_next = NpNode_ZNContact.pool;
    NpNode_ZNContact.pool = old;
};
        return cur;
    }
    
    
    public inline function splice(pre:NpNode_ZNContact,cur:NpNode_ZNContact,n:Int):NpNode_ZNContact {
        while(n-->0 && cur!=null) cur = erase(pre,cur);
        return cur;
    }
    
    
    public inline function clear() {
        if(true) {
            while(!empty()) {
                var old = begin();
                 head=old.next;
                  {
    old.elem().free();
    old.elem().pool_next = ZNContact.pool;
    ZNContact.pool = old.elem();
};
                  {
    old.free();
    old.pool_next = NpNode_ZNContact.pool;
    NpNode_ZNContact.pool = old;
};
            }
        }
    }
    
    
    
    
    public inline function reverse() {
        var cur = begin();
        var pre = null;
        while(cur!=null) {
            var nx = cur.next;
            cur.next = pre;
             head=cur;
            pre = cur;
            cur = nx;
        }
    }
    
    
    
    
    public inline function empty():Bool return begin()==null
    
    
    public inline function size() {
        var cnt = 0;
        var cur = begin();
        while(cur!=null) { cnt++; cur = cur.next; }
        return cnt;
    }
    
    
    public inline function has(obj:ZNContact) return  ({
    var ret = false;
      {
    var npiterator = this.begin();
    while(npiterator != null) {
        var npite = npiterator.elem();
        {
            
            
            {
        if(npite==obj) {
            ret = true;
            break;
        }
    };
        }
        npiterator = npiterator.next;
    }
};
    ret;
})
    
    
    
    
    public inline function front() return begin().elem()
    
    
    public inline function back() {
        var ret = begin();
        var cur = ret;
        while(cur!=null) { ret = cur; cur = cur.next; }
        return ret.elem();
    }
    
    
    public inline function iterator_at(ind:Int) {
        var ret = begin();
        while(ind-->0 && ret!=null) ret = ret.next;
        return ret;
    }
    
    
    public inline function at(ind:Int) {
        var it = iterator_at(ind);
        return if(it!=null) it.elem() else null;
    }
    

}